package com.e3.web.index.client;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.e3.web.index.model.AccordionModel;
import com.e3.web.index.model.NavigatorModel;
import com.extjs.gxt.ui.client.util.DateWrapper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;


public class NavigatorHelper {
	
	private static MainFrame frame;
	
	public static void setMainFrame( MainFrame frame){
		NavigatorHelper.frame = frame;
	}

	public static void onElementSelected(MainFrameUI mainFrame,NavigatorModel module,String text) {
		
		Map<String,String> paras = new HashMap<String, String>();
		paras.put("userCode", "111");
		paras.put("moduleCode", module.getCode());
		paras.put("moduleName", module.getName());
		paras.put("modulePath", module.getPath());
		
		DateWrapper wrapper = new DateWrapper(new Date());
		paras.put("time", wrapper.getTime()+"");
		
		String paraUrl = parasToUrl(paras);
		String url = GWT.getHostPageBaseURL()+"Frame.jsp?"+paraUrl;
		
		mainFrame.getWorkspacePanel().openWorkspace(text,url);
	}
	
	public static void onElementSelected(AccordionModel module,String text) {
		String url = getModuleURL(module);		
		frame.getWorkspacePanel().openWorkspace(text,url);
	}
	
	public static String getModuleURL(AccordionModel module) {
		Map<String,String> paras = new HashMap<String, String>();
		paras.put("moduleCode", module.getCode());
		paras.put("moduleName", module.getName());
		paras.put("modulePath", module.getPath());
		
		DateWrapper wrapper = new DateWrapper(new Date());
		paras.put("time", wrapper.getTime()+"");
		
		String paraUrl = parasToUrl(paras);
		String url = GWT.getHostPageBaseURL()+"Frame.jsp?"+paraUrl;
		
		return url;
	}
	
	public static String getInfoCenterUrl() {
		AccordionModel module = new AccordionModel();
		module.setCode(MainMessages.INSTANCE.infoc_code());
		module.setName(MainMessages.INSTANCE.infoc_name());
		module.setPath(MainMessages.INSTANCE.infoc_url());
		return getModuleURL(module);
	}
	
	public static String getInfoCenterTitle(){
		return MainMessages.INSTANCE.infoc_name();
	}

	private static String parasToUrl(Map<String,String> paras){
		StringBuffer url = new StringBuffer();
		Iterator<String> it = paras.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			String value = paras.get(key);
			url.append(key+"="+value+"&");
		}
		return url.toString().substring(0,url.length()-1);
	}
	
	private void sendRequest(String basicURL,String data){
        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, basicURL);
        builder.setHeader("Content-Type", "text/x-gwt-rpc; charset=utf-8");
        builder.setRequestData(data);
        builder.setCallback(new RequestCallback(){
            public void onError(Request request, Throwable exception) {
            }
            public void onResponseReceived(Request request, Response response) {
               if(200 == response.getStatusCode()){
                }else{
                }
            }
        });
        try {
            builder.send();
        } catch (RequestException e) {
            e.printStackTrace();
        }
	}
}
